package com.example.administrator.bitmapcache;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**加载网络图片
 * Created by Administrator on 2017/11/15 0015.
 */
public class LoadPhotoThread implements Runnable {
    Context context;
    String urlStr;

    public LoadPhotoThread(Context context, String url){
        this.context = context;
        this.urlStr = url;
    }

    @Override
    public void run() {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(8000);//连接超时8s
            conn.setReadTimeout(30000);//设置读取超时30s
            conn.setRequestMethod("GET");
            Log.d("Context","getResponseCode:"+conn.getResponseCode());
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
            }
            //状态非200
            else {
                Log.d("Context","失败！");
            }
        }
        catch (Exception e){
            Log.d("Context","获取广告图片出错："+e);
        }
    }
}
